package com.leyou.item.controller;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {
    /**
     * 构造函数进行依赖注入
     */
//    private final BrandService brandService;
//
//    public BrandController(BrandService brandService) {
//        this.brandService = brandService;
//    }
    @Autowired
    BrandService brandService;

    /**
     * 根据id查询品牌
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }

    /**
     * 根据品牌id集合查询品牌集合
     *
     * @param idList id集合
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> idList) {
        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(idList)));

    }

    /**
     * @param key  查询条件
     * @param page 当前页
     * @param rows 每页显示条数
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> quereyBrandByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows) {
        return ResponseEntity.ok(brandService.queryBrandByPage(page, rows, key));
    }

    /**
     * 根据分类id查询品牌
     *
     * @param id
     * @return
     */

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id") Long id) {
        return ResponseEntity.ok(brandService.queryBrandByCategory(id));
    }

    /**
     * 新增品牌
     *
     * @param brandDTO brandDTO 品牌对象
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO) {
          brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改品牌
     * @param brandDTO 品牌及分类信息
     * @return
     */
     @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
     }

    /**
     * 删除品牌
     * @param id 要删除的品牌id
     * @return
     */
     @DeleteMapping("/{id}")
    public  ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id ){
         brandService.deleteBrandById(id);
         return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
     }
}